var apps, views, user;
mui.plusReady(function () {
   views = bexta.getWebview();
   user = bexta.getStorage(USER);
// console.log(bexta.jsonToString(views.getStyle()))
	
   getData({
   	   uid:user.uid,
   	   type:views.orderType,
   	   firstrow:0,
   	   listrow:10
   }, function(res) {
   		
		initVue(res);   		
   });
});
//初始化vue
function initVue(res){
	apps = new Vue({
		el:"#vueMain",
		data:{
			type:views.orderType,   //当前的type
			first:res.length ,
			last:10,
			list:res
		},
		mounted:function(){
			document.querySelector('.loading').classList.add('loadRun');
			bexta.imageLoad('[data-load]');
			initEvent();
			//未确认时取消订单
			mui('#vueMain').on('tap', '.cancleOrder', function(){
				var idx = this.getAttribute('data-idx');
				var data = apps.list[idx].order_no;
				bexta.ajax(api.del_order, function(res,msg){
					mui.toast(msg);
					if( !res ){
						apps.list.splice(idx, 1);
						//重置上拉加载
						mui('#refreshContainer').pullRefresh().refresh(true);
					}
				},{data:{uid:user.uid, order_no:data, type:3}});
			});
			//单个支付
			mui('#vueMain').on('tap', '.fuweikuan', function(){
				var order = this.getAttribute('data-order');
				bexta.ajax(api.update_order, function(res){
					bexta.openWithTitle({
						id:'pay',
						url:'pay.html',
						extras:{
							data:res
						}
					}, {
						title:{
							text:"订单支付"
						}
					});
				}, { data:{uid:user.uid, order_no:order, is_pay:1}});
			});
			//单个统计
			mui('#vueMain').on('tap', '.mui-checkbox', function(){
				var that = this;
				mui.later(function(){
					countListChecked();
				}, 50)
			});
			
			
		},
		updated:function(){
			bexta.imageLoad('[data-load]');
		}
	})
}
//获取数据
function getData(obj, func) {
	bexta.ajax(api.order_list, func, {data:obj});
}
//tab事件
window.addEventListener('toggleTab', function(evt){
	var type = evt.detail.orderType;
	getData({
		uid:user.uid,
   	   	type:type,
   	   	firstrow:0,
   	   	listrow:10
	}, function(res){
		apps.first = res.length;
		apps.list = res;
		apps.type = type;
		//重置上拉加载
		mui('#refreshContainer').pullRefresh().refresh(true);
	});
});
//全选事件
window.addEventListener('toggleCheckbox', function(evt){
	var a = evt.detail.flag;
	mui.each(document.querySelectorAll('.listCheckbox input'), function(){
		this.checked = a==1?true:false;
	});
	countListChecked();
});
//结算事件
window.addEventListener('wind', function(){
	var order = [];
	mui.each(document.querySelectorAll('.listCheckbox input'), function(){
		if( this.checked ){
			var idx = this.value;
			order.push(apps.list[idx].order_no);
		}
	});
	if( order.length < 1 ){
		mui.toast("请勾选要要结算的订单");
		return ;
	}
	bexta.ajax(api.update_order, function(res){
		bexta.openWithTitle({
			id:'pay',
			url:'pay.html',
			extras:{
				data:res
			}
		}, {
			title:{
				text:"订单支付"
			}
		});
	},{data:{uid:bexta.getStorage(USER).uid, order_no:order.join(','),is_pay:1}});
});
//退款成功回调
window.addEventListener('reloadOrder', function(evt){
	var o = evt.detail.order_no;
	mui.each(apps.list, function(index){
		if( this.order_no == o ){
			apps.list.splice(index,1);
		}
	});
});
//统计选中的项目
function countListChecked(){
	var num = 0, count = 0;
	mui.each(document.querySelectorAll('.listCheckbox input'), function(){
		if( this.checked ){
			var idx = this.value;
			num++;
			count += parseFloat(apps.list[idx].final_pay);
		}
	});
	mui.fire(views.opener(),'showCont', {
		nums:num,
		counts:count
	});
}
//初始化
function initEvent(){
	mui.init({
  pullRefresh : {
    container:'#refreshContainer',
    down : {
      callback :function(){
      		var self = this;
      		getData({
      			uid:user.uid,
		   	   	type:apps.type,
		   	   	firstrow:0,
		   	   	listrow:apps.first
      		}, function(res){
      			apps.list = res;
      			mui('#refreshContainer').pullRefresh().endPulldownToRefresh();
      		});
      }
    },
    up : {
      callback :function(){
      		var self = this;
      		getData({
      			uid:user.uid,
		   	   	type:apps.type,
		   	   	firstrow:apps.first,
		   	   	listrow:apps.last
      		}, function(res){
      			if( res.length == 0 || res.length < apps.last ){
					self.endPullupToRefresh(true);
				}
				if( res.length == apps.last ){
					self.endPullupToRefresh(false);
				}
				if( res.length !=0 ){
					apps.first = apps.first + res.length;
					mui.each( res, function(){
						apps.list.push( this );
					} )
				}
      		});
      }
    }
  }
});
}
